<?php

class IndexController extends Zend_Controller_Action {

    public function init() {
        
        $this->_helper->layout()->setLayout('index-layout');

        
    }

    public function indexAction() {
        $this->view->search="inline";

        $itemsManager = new Managers\Items();
        $this->view->items = $itemsManager->getList();
        $array = $itemsManager->getList();
        //      $curpage=1;  //potochna storinka
        $pages = 10; //kilkist  storinok sho vuvodyatsya max
        $data = 5; //kilkist elementiv na storinci

        $pageCount = count($array);
        $paginator = new Zend_Paginator(new Zend_Paginator_Adapter_Array($this->view->items));

        $paginator->setCurrentPageNumber($this->_getParam('page'));
        $paginator->setPageRange($pageCount);
        $paginator->setItemCountPerPage($data);

        $this->view->paginator = $paginator;


        //        print_r($this->view->items);

        //  $array=$itemsManager->getList();

        /*           $array = array();
                      for ($i=0;$i<100;$i++)
                      {
                       $array[$i]=$i;
                      }
        */

        /*
*
      $itemsManager = new Managers\Items();

      $tagsManager = new Managers\Tags();

      if ($this->_request->isPost()) {

          $connects = new Managers\Connects();
          $tags_param = $this->_request->getParam('tags');

          $item = $itemsManager->add($this->_getParam('item'));

          foreach ($tags_param as $tag_param) {

              $tag = $tagsManager->getById($tag_param, false);
              $connects->add($item, $tag);
          }
      }

      $this->view->items = $itemsManager->ary();

      $this->view->tags = $tagsManager->getList();*/

    }

    public function viewAction() {
        $itemManager = new \Managers\Items();
        $connectsManager = new \Managers\Connects();

        $session = new Zend_Session_Namespace('default');
        $tags = $session->tags;

        $similar = $connectsManager->search($tags, 5);
        /**
         * Получаем тэги
         */
        foreach ($similar as $key => $value) {

            $similar[$key]['tags'] = $connectsManager->getTags($value['id']);
        }

        $this->view->item = $itemManager->getOneItem($this->_getParam('id'));
        $this->view->similar = $similar;

    }

    public function showAction() {

        $page = $this->_getParam('page');
        $tag = $this->_getParam('tag');

        $connectsManager = new \Managers\Connects();

        if ($page) {
            $this->view->items = $connectsManager->getItemById($page);
        } else {
            $this->view->items = $connectsManager->getTagById($tag);
        }
    }

    public function tagsAction() {

        $tagsManager = new \Managers\TagsGroup();

        $this->view->groups = $tagsManager->getList();
    }

    /**
     * Action смены языка (локали).
     * @return void
     */
    public function languageAction() {

        $session = new Zend_Session_Namespace('default');

        $locale = $this->_getParam('locale');

        $session->locale = $locale;

        $this->_redirect('/');
    }

}
